<template>
  <div class="page">
    <!-- <el-form :model="searchForm" label-position="left" inline label-suffix="：" size="small">
      <div class="flex wap flex-start">
        <el-form-item label="名称">
          <el-input v-model="searchForm.name"></el-input>
        </el-form-item>
        <el-form-item label="手机号码">
          <el-input v-model="searchForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="">
          <el-button type="primary" @click="getData">查询</el-button>
        </el-form-item>
      </div>
    </el-form> -->
    <div class="flex wap flex-start" style="padding: 0px 0 10px 0">
      <el-button type="success" @click="addPic">新增图片</el-button>
      <div style="width:20px"></div>
      <!-- <el-button type="danger" @click="del">删除</el-button> -->
    </div>
    <el-table :data="tableData" border style="width: 100%"  height="600"  :header-cell-style="{
        background: '#eef1f6',
        color: '#606266',
        'text-align': 'center'
      }"
      :cell-style="{ 'text-align': 'center' }">
      <!-- <el-table-column type="selection"></el-table-column> -->
      <!-- <el-table-column prop="imgid" label="ID"></el-table-column> -->
      <el-table-column prop="imgindex" label="图片序号" width="150"></el-table-column>
      <el-table-column prop="isEffect" label="是否生效" width="150">>
        <template slot-scope="scop">
          <el-tag type="success" v-if="scop.row.isEffect == 1">生效</el-tag>
          <el-tag type="warning" v-if="scop.row.isEffect == 2">失效</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="isEffect" label="预览" width="150">
        <template slot-scope="scop">
          <el-image fit="fil" style="width: 80px; height: 50px" :src="scop.row.imgurl.indexOf('http')>-1?scop.row.imgurl:$Url + '/upload/' + scop.row.imgurl"
            :preview-src-list="[scop.row.imgurl.indexOf('http')>-1?scop.row.imgurl:$Url + '/upload/' + scop.row.imgurl]">
          </el-image>
        </template>
      </el-table-column>
       <el-table-column prop="imgsrc" label="链接">
        <template slot-scope="scop">
          <el-button type="text" @click="goLink(scop.row.imgsrc)">{{scop.row.imgsrc}}</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="createtime" label="上传时间"></el-table-column>
      <el-table-column prop="updatetime" label="更新时间" width="200"></el-table-column>
      <el-table-column prop="" label="操作" width="120">
        <template slot-scope="scop">
          <el-button type="primary" icon="el-icon-edit" circle @click="edit(scop.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle @click="del(scop.row.imgid)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="float:right;padding:10px">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[10, 20, 50, 100, 200]"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
    <el-dialog title="H5首页图片" center :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="addForm" ref="addSetForm" :rules="rule" label-width="100px" class="edit-form" label-suffix="：">
        <el-form-item label="图片内容" prop="imgurl">
          <upload v-model="addForm.imgurl" :disabled="addForm.imgid" />
        </el-form-item>
        <el-form-item label="显示序号" prop="imgindex">
          <el-input v-model="addForm.imgindex" placeholder="请输入图片序号" clear></el-input>
        </el-form-item>
        <el-form-item label="超链接" prop="imgsrc">
          <el-input v-model="addForm.imgsrc" placeholder="请输入图片超链接" clear></el-input>
        </el-form-item>
        <el-form-item label="是否生效" prop="isEffect">
          <el-switch v-model="addForm.isEffect" :active-value="1" :inactive-value="2" active-text="是" inactive-text="否"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addSave">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  getImgConfig,
  addImgConfig,
  updateImgConfig,
  delImgConfig
} from "@/api/user";
import upload from "@/components/uploadFile";
import {UrlReg} from "@/utils/validate";

export default {
  components: {
    upload
  },
  data() {
    return {
      total: 0,
      dialogFormVisible: false,
      pageNum: 1,
      pageSize: 10,
      tableData: [],
      searchForm: {
        name: "",
        phone: ""
      },
      addForm: {
        imgindex: "",
        imgurl: "",
        imgsrc: '',
        isEffect: 2,
        imgid: "",
      },
      rule: {
        imgurl: [
          { required: true, message: '必须上传图片', trigger: 'change' }
        ],
        imgindex: [
          { required: true, message: '必须设置排序', trigger: 'change' }
        ],
        imgsrc: [
          { pattern: UrlReg, message: '链接格式例如：http://www.baidu.com', trigger: 'change'}
        ]
      },
      
    };
  },
  computed: {
    queryData() {
      return {
        pageNum: this.pageNum,
        pageSize: this.pageSize
      };
    },
    addData() {
      const { imgindex, imgurl, isEffect,imgsrc } = this.addForm;
      return {
        imgindex,
        imgurl,
        imgsrc,
        isEffect
      };
    },
    updataData() {
      const { imgindex, imgurl, imgid, isEffect,imgsrc } = this.addForm;
      return {
        imgid,
        imgurl,
        imgsrc,
        imgindex,
        isEffect
      };
    }
  },
  mounted() {
    this.getData();
  },
  methods: {
    async getData() {
      const res = await getImgConfig.post(this.queryData);
      if (res.resultCode === "000000") {
        this.tableData = res.data.imgConfigList;
        this.total = res.data.count;
      } else {
        this.$message.error(res.resultInfo);
      }
    },
    goLink(rul){
      window.open(rul)
    },
    addPic() {
      this.addForm = {
        imgindex: "",
        imgurl: "",
        imgsrc: '',
        isEffect: "2",
        imgid: "",
      },
      this.dialogFormVisible = true;
    },
    async addSave() {
      this.$refs.addSetForm.validate(async (valid) => {
        if (!valid) return false
        if (this.addForm.imgid) {
          const res = await updateImgConfig.post(this.updataData);
          if (res.resultCode === "000000") {
            this.dialogFormVisible = false;
            this.$message.success("修改成功！");
            this.getData();
          } else {
            this.$message.error(res.resultInfo);
          }
        } else {
          const res = await addImgConfig.post(this.addData);
          if (res.resultCode === "000000") {
            this.dialogFormVisible = false;
            this.$message.success("添加成功！");
            this.getData();
          } else {
            this.$message.error(res.resultInfo);
          }
        }
      })
    },
    edit(row) {
      const { imgindex, imgurl, isEffect, imgid, imgsrc } = row;
      this.addForm = {
        imgindex,
        imgurl,
        imgsrc,
        isEffect,
        imgid
      },
      this.dialogFormVisible = true;
    },
    del(imgid) {
      this.$confirm("此操作将删除该项目, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.delImg(imgid);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    async delImg(imgid) {
      const res = await delImgConfig.post({ imgid });
      if (res.resultCode === "000000") {
        this.$message.success("删除成功！");
        this.getData();
      } else {
        this.$message.error(res.resultInfo);
      }
    },
    handleSizeChange(val) {
      this.pageNum = 1;
      this.pageSize = val;
      this.getData();
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.getData();
    }
  }
};
</script>
<style lang="scss" scoped>
</style>
